#pragma once
#include "unordered_set"

#include  "iostream"
#include  "vector"
#include  "stack"
#include  "unordered_map"
#include   "queue"

using namespace std;

//todo 抄的 使用递归来做
// 出口如果 s.size()<k return 0
//函数作用s中至少k个最长子串

vector<string> split(char c, const string &s) {
    vector<string> res;
    string t;
    for (const auto &item : s) {

        //string s="baaabcb";
        if (item == c && !t.empty()) {
            res.push_back(t);
            t.clear();
        } else if (item != c) {
            t.push_back(item);
        }
    }

    if (!t.empty()) {
        if (t.back() == c)
            t.pop_back();

        if (!t.empty())
            res.push_back(t);
    }


    return res;

}


int longestSubstring(string s, int k) {
    if (s.size() < k)
        return 0;

    unordered_map<char, int> counter;
    unordered_set<char> chars(s.begin(), s.end());

    for (const auto &item : s) {
        ++counter[item];
    }

    for (const auto &item : chars) {
        //如果发现不够数就根据这个字符进行分割
        if (counter[item] < k) {
            auto vec = split(item, s);

            int ret = 0;
            for (const auto &c : vec) {

                ret = max(ret, longestSubstring(c, k));
            }

            return ret;

        }


    }


    return s.size();


}